/*
 * @(#)StringUtils.java 
 * Project: APEP-JZCG 
 *
 * Modify Information:
 * =============================================================================
 *   Author       Date       Description
 *   ------------ ---------- ---------------------------------------------------
 *   john        2008-08-08  First release
 *
 * Copyright Notice:
 * =============================================================================
 *    Copyright (c) 2001-2007 Auto-Parts E-Procurement Co., Ltd. 
 *        No.251-1 Wuning Road, Shanghai, 200063, China All rights reserved.
 *
 *    This software is the confidential and proprietary information of
 *        Auto-Parts E-Procurement Co., Ltd. ("Confidential Information").
 *        You shall not disclose such Confidential Information and shall use it
 *        only in accordance with the terms of the license agreement you entered
 *        into with APEP.
 *
 * Warning:
 * =============================================================================
 *   
 * 
 */
package com.hitrust.gs.util.lang;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.text.DecimalFormat;

import com.hitrust.gs.lib.GSLogWriter;

public class StringUtils {

    /**
     * �Ʀ数字格式化函数�ƨ��
     * 
     * @param number:
     *            �格式化前的数字;
     * @param decimalDigits:小数位数�Ʀ��;
     * @return: �三位一组以逗号分割的字符串�Ŧ�;
     */
    public static String format(double number, int decimalDigits) {
        String tstr = Double.toString(number);
        return fmtDbl(tstr, decimalDigits);
        /*
         * if (number == 0d) number = 0d;
         * 
         * boolean flag=false; if(decimalDigits < 0) {
         * GSLogWriter.error("StringUtils : 小数位数不能小于0。"); return ""; }
         * 
         * String pattern = "###,###,###,###,###,###"; if(decimalDigits > 0) {
         * flag=true; pattern += "."; for(int i=0;i<decimalDigits;i++) {
         * pattern += "0"; } }
         * 
         * DecimalFormat df = new DecimalFormat(pattern); if (number <= -1d){
         * return df.format(number); }else if (number > -1d && number < 0d){
         * return "-0"+df.format(number).substring(1); }else if (number >= 0d &&
         * number < 1d){ if(flag==true){ return "0"+df.format(number); }else{
         * return df.format(number); } }else{ return df.format(number); }
         */
    }

    /**
     * �Ʀ数字格式化函数�ƨ��
     * 
     * @param s:
     *            �格式化前的字符串（是一个数字）;
     * @param decimalDigits:
     *            小数位数;
     * @return:三位一组以逗号分割的字符串，如果为null或空串或只有空格的字符串，返回空串�Ŧ�;
     */
    public static String format(String s, int decimalDigits) {
        return fmtDbl(s, decimalDigits);
        /*
         * if(s==null||s.trim().length()==0||s.trim().equalsIgnoreCase("&nbsp;")){
         * if(decimalDigits > 0) { String pattern = "0."; for(int i=0;i<decimalDigits;i++) {
         * pattern += "0"; } return pattern; }else{ return "0.00"; } } double
         * number = Double.parseDouble(s); return format(number,decimalDigits);
         */
    }

    /**
     * �Ʀ验证是否符合指定小数位数标准的�ƨ��小数
     * 
     * @param s:
     *            �字符串（是一个数字）;
     * @param decimalDigits:
     *            小数位数;
     * @return:true 是/false 不是
     */
    public static boolean checkDecimal(String s, int decimalDigits) {
        if (s == null || s.trim().length() == 0
                || s.trim().equalsIgnoreCase("&nbsp;")) {
            return false;
        }
        int posOfDot = s.indexOf(".");
        if (posOfDot != -1) {
            if (s.length() - posOfDot - 1 > decimalDigits) {
                return false;
            }
        }
        return true;
    }

    /**
     * 数字格式化函数
     * 
     * @param number:�以科学计数法表示的doulbe数值���ƭ�
     * @param decimalDigits:��ܪ��显示的小数位数�Ʀ��
     * @return �以文本方式显示的double数值����
     */
    public static String formatEDouble(double number, int decimalDigits) {
        String s="0.";
        if (decimalDigits<=0) {
            s="0";
        }
        for (int i = 1; i <=decimalDigits; i++) {
            s+="0";
        }
        DecimalFormat df=new DecimalFormat(s);
        return df.format(number);
        /*
         * if (number == 0d) number = 0d;
         * 
         * boolean flag=false; if(decimalDigits < 0) {
         * GSLogWriter.error("StringUtils : 小数位数不能小于0。"); return ""; }
         * 
         * String pattern = "##################"; if(decimalDigits > 0) {
         * flag=true; pattern += "."; for(int i=0;i<decimalDigits;i++) {
         * pattern += "0"; } }
         * 
         * DecimalFormat df = new DecimalFormat(pattern); if (number <= -1d){
         * return df.format(number); }else if (number > -1d && number < 0d){
         * return "-0"+df.format(number).substring(1); }else if (number >= 0d &&
         * number < 1d){ if(flag==true){ return "0"+df.format(number); }else{
         * return df.format(number); } }else{ return df.format(number); }
         */
    }

    /**
     * 数字格式化函数
     * 
     * @param str
     *            以科学计数法表示的字符串
     * @return double数值
     */
    public static double parseDouble(String str) {
        try {
            return Double.parseDouble(str);
        } catch (Exception e) {
            GSLogWriter.error("StringUtils : " + e.toString());
            return 0d;
        }
    }

    /**
     * 检查字符范围
     * 
     * @param aCheckString
     *            检查的字符
     * @param aCheckChar
     *            检查字符的限定范围
     * @return true/false false表示检查字符在限定范围之内/true表示检查字符在限定范围之外
     */
    public static boolean checkChar(String aCheckString, String aCheckChar) {
        int j = 0;

        for (int i = 0; i < aCheckString.length(); i++) {
            int c = aCheckString.charAt(i);
            if (aCheckChar.indexOf(c) < 0) {
                j++;
                break;
            }
        }
        if (j == 0) {
            return false;
        } else {
            return true;
        }
    }

    /**
     * 检查是否是数字和点
     * 
     * @param aCheckString
     *            要检查的字符串
     * @return true 是/false 不是
     */
    public static boolean isNumberAndDot(String aCheckString) {
        String icheckChar = "1234567890.";
        if (checkChar(aCheckString, icheckChar)) {
            return false;
        }
        return true;
    }

    /**
     * Checks if the string contains only unicode digits. Null will return null.
     * 
     * @param aStr
     *            the string to check
     * @return true if only contains digits, and is non-null
     */
    public static boolean isNumeric(String aStr) {

        if (aStr == null) {
            return false;
        }
        int tSz = aStr.length();
        for (int i = 0; i < tSz; i++) {
            if (Character.isDigit(aStr.charAt(i)) == false) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断是否是Email
     * 
     * @param aMail
     * @return
     */
    public static boolean isMail(String aMail) {
        if (aMail == null || "".equals(aMail.trim()))
            return false;
        if (aMail.charAt(0) == '.' || aMail.charAt(0) == '@'
                || aMail.indexOf("@") == -1 || aMail.indexOf(".") == -1)
            return false;
        return true;
    }

    /**
     * 检查金额长度
     * 
     * @param aAmount
     *            金额
     * @param aExponent
     *            指数
     * @return 大于12位返回false，否则返回true
     */
    public static boolean checkLength(String aAmount, int aExponent) {
        String tResult = "";
        double tAmount = 0;
        boolean tFlag = false;
        tAmount = StringUtils.parseDouble(aAmount);
        tAmount = tAmount * Math.pow(10, aExponent);
        tResult = String.valueOf(Math.round(tAmount));
        if (tResult.length() <= 12) {
            tFlag = true;
        }
        return tFlag;
    }

    public static String convertEncode(String message) {
        if (message != null && message.trim().length() > 0) {
            try {
                return new String(message.trim().getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return message;
    }

    public static String escapeXML(String text) {
        if (text == null)
            return null;
        if (text.length() < 1)
            return text;
        StringBuffer result = new StringBuffer("");
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (c == '&') {
                result = result.append("&amp;");
            } else if (c == '<') {
                result = result.append("&lt;");
            } else if (c == '>') {
                result = result.append("&gt;");
            } else if (c == '"') {
                result = result.append("&#034;");
            } else if (c == '\'') {
                result = result.append("&#039;");
            } else {
                result = result.append(c);
            }
        }
        return result.toString();
    }

    public static String escapeXMLWithSlash(String text) {
        if (text == null)
            return null;
        if (text.length() < 1)
            return text;
        StringBuffer result = new StringBuffer("");
        for (int i = 0; i < text.length(); i++) {
            char c = text.charAt(i);
            if (c == '&') {
                result = result.append("\\&");
            } else if (c == '<') {
                result = result.append("\\<");
            } else if (c == '>') {
                result = result.append("\\>");
            } else if (c == '"') {
                result = result.append("\\\"");
            } else if (c == '\'') {
                result = result.append("\\\'");
            } else {
                result = result.append(c);
            }
        }
        return result.toString();
    }

    public static String fmtDbl(String value) {
        String rt = "";
        BigDecimal bd = new BigDecimal(value);
        long tlng;
        bd = bd.movePointRight(2);
        tlng = bd.longValue();
        bd = new BigDecimal(tlng);
        bd = bd.movePointLeft(2);
        rt = bd.toString();
        return rt;
    }

    public static String fmtDbl(String value, int decimalDigits) {
        String rt = "";
        BigDecimal bd = new BigDecimal(value);
        long tlng;
        bd = bd.movePointRight(decimalDigits);
        tlng = bd.longValue();
        bd = new BigDecimal(tlng);
        bd = bd.movePointLeft(decimalDigits);
        rt = bd.toString();
        return rt;
    }

    public static void main(String[] args) {
        //System.out.println(StringUtils.checkDecimal("1200", 2));
        System.out.println(StringUtils.deCode("&#x4F60;&#x597D;"));
    }

    public static String deCode(String str) {
        if (str == null)
            return "";
        String temp = "";
        String return_temp = "";
        try {
            int str_len = str.length() / 7;
            for (int i = 1; i <= str_len; i++)
                temp += str.substring(3 * i + 5 * (i - 1), 7 * i + (i - 1));
            temp = "FEFF" + temp;
            temp = temp.toLowerCase();
            int temp_len = temp.length() / 2;
            byte mm[] = new byte[temp_len];
            String ss[] = new String[temp_len];
            for (int i = 0; i < temp_len; i++) {
                String qq = temp.substring(i * 2, i * 2 + 1);
                if (!qq.equals("0"))
                    ss[i] = temp.substring(i * 2, i * 2 + 2);
                else
                    ss[i] = temp.substring(i * 2 + 1, i * 2 + 2);
                mm[i] = (byte) Integer.parseInt(ss[i], 16);
                System.out.println(mm[i]);
            }
            return_temp = new String(mm, "UTF-16");
        } catch (Exception e) {
            System.out.print(e.getMessage());
        }
        return return_temp;
    }



}
